/*
 * 渠道列表
 */

//列表每页显示条数
var perPage = 10;

//是否请求完成标识
var loadBool = true;

// 页面初始化
$(function () {
    list();
});

// 查询渠道
$('#query').click(function () {
    list();
});

// 清空
$(document).delegate("#clear", "click", function () {
    $("#name").val('');
    $("#start_time").val('');
    $("#end_time").val('');
    $("#list").empty();
    list();
});

function list() {
    $("#curPage").text(0);
    $("#pages").text(0);
    getChannelList(1);
}

//下一页
$(document).delegate("#nextPage", "click", function () {
    var curPage = Number($("#curPage").text());
    var pages = Number($("#pages").text());
    if (curPage == pages) {
        return;
    }
    getChannelList(curPage + 1);
});

//上一页
$(document).delegate("#lastPage", "click", function () {
    var curPage = Number($("#curPage").text());
    if (curPage <= 1) {
        return;
    }
    getChannelList(curPage - 1);
});

//获取渠道列表
function getChannelList(curPage) {
    if (!loadBool) {
        return;
    }
    loadBool = false;
    $("#list").empty();
    //loading层
    var layerIndex = layer.load(2, {
        shade: [0.1, '#fff'] //0.1透明度的白色背景
    });

    var index = curPage - 1;
    var params = JSON.stringify({
        "name": $("#name").val(),
        "start_time": $("#start_time").val(),
        "end_time": $("#end_time").val(),
        'pos': index * perPage,
        'limit': perPage
    });
    $.ajax({
        type: "POST",
        url: "/channellist/glist",
        contentType: "application/json",
        dataType: "json",
        data: params,
        success: function (data) {
            layer.close(layerIndex);
            if (data.status == 0) {
                $("#pages").text(Math.ceil(data.count / perPage));
                if (data.list.length > 0) {
                    $("#curPage").text(index + 1);
                    for (var i = 0; i < data.list.length; i++) {
                        var row = "<tr>" +
                                "<td>" + data.list[i].channel + "</td>" +
                                "<td>" + data.list[i].name + "</td>" +
                                "<td>" + data.list[i].create_time + "</td>" +
                                "</tr>";
                        $("#list").append(row);
                    }
                }
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
            loadBool = true;
        },
        error: function () {
            layer.close(layerIndex);
            bbx_alert_error("出错啦~");
            loadBool = true;
        }
    });
}

